package com.cos.hospital.tool.takephoto;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.cos.hospital.R;
import com.cos.hospital.code.Code;
import com.cos.hospital.init.BaseActivity;
import com.cos.hospital.tool.FileUtil;
import com.cos.hospital.tool.glide.GlideApp;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import me.kareluo.imaging.IMGEditActivity;

/**
 * 拍照
 */
public class PreviewActivity extends BaseActivity {
    private static final int REQUEST_EDIT_IMAGE_CODE = 99;
    @BindView(R.id.iv_photo)
    ImageView iv_photo;
    @BindView(R.id.iv_cancel)
    ImageView iv_cancel;
    private String photoPath;
    private String targerFilePath;

    @Override
    protected int layout() {
        return R.layout.activity_perview;
    }

    @Override
    protected void init() {
        photoPath = getIntent().getStringExtra(Code.SP.IMAGEPATH);
        if (photoPath != null) {
            showImage();
        }
    }

    @Override
    protected void logic() {
    }

    @OnClick(R.id.iv_cancel)
    public void cancel() {
        finish();
    }

    @OnClick({R.id.iv_edit})
    public void editImage() {
        targerFilePath = FileUtil.getExternalCacheDir(this) + File.separator + System.currentTimeMillis() + ".jpg";
        Intent intent = new Intent(this, IMGEditActivity.class);
        intent.putExtra(IMGEditActivity.EXTRA_IMAGE_URI, Uri.fromFile(new File(photoPath)));
        intent.putExtra(IMGEditActivity.EXTRA_IMAGE_SAVE_PATH, targerFilePath);
        startActivityForResult(intent, REQUEST_EDIT_IMAGE_CODE);
    }

    @OnClick({R.id.iv_confirm})
    public void confirm() {
        Intent intent = new Intent();
        intent.putExtra(Code.SP.SAVEPATH, photoPath);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                //图片编辑
                case REQUEST_EDIT_IMAGE_CODE:
                    photoPath = targerFilePath;
                    showImage();
                    break;
            }
        }
    }

    private void showImage() {
        GlideApp.with(this)
            .load(photoPath)
            .fitCenter()
            .into(iv_photo);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
    }

}
